package tool

import "encoding/json"

func Clone(model interface{}, ptr interface{}) error {
	str, err := json.Marshal(model)
	if err != nil {
		return err
	}
	mapper := make(map[string]interface{})
	err = json.Unmarshal(str, &mapper)
	if err != nil {
		return err
	}
	delete(mapper, "id")
	noIdStr, err := json.Marshal(mapper)
	err = json.Unmarshal(noIdStr, &ptr)
	if err != nil {
		return err
	}
	return nil
}

func GetModelField(model interface{}, field string) interface{} {
	str, err := json.Marshal(model)
	if err != nil {
		return nil
	}
	println(string(str))
	mapper := make(map[string]interface{})
	err = json.Unmarshal(str, &mapper)
	if err != nil {
		return nil
	}
	find, ok := mapper[field]
	if !ok {
		return nil
	}
	return find
}
